using System;
using System.ComponentModel;

using System.Web.UI;
using SurveyHost.Core.Controls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ECSCustomQuestions
{
    [ToolboxItem(false)]
    [ValidationProperty("AnswerText")]
    public class MMEA : BaseSurveyQuestion
    {
        private RadioButtonList buttonGroup1;
        private RadioButtonList buttonGroup2;
        private LiteralControl literal;
        private const string _helpText = @"This question type is designed for the MMEA measure and has no configuration.  It consists of a question and two groups of radio buttons.
Each group consists of eight options, numbered and scored 1 through 7 and 0.  Answers are saved to the same cell.  The two choices will be separated by the pipe character: |";

        public override string ConfigurationHelpText
        {
            get
            {
                return _helpText;
            }
        }

        public MMEA()
        {
        }

        /// <summary>
        /// Creates one of the parts of the radio button list
        /// </summary>
        /// <returns></returns>
        private RadioButtonList CreateButtonGroup()
        {
            RadioButtonList retval = new RadioButtonList();
            ListItem i;
            i = new ListItem("1", "1");
            i.Attributes.Add("title", "Once");
            retval.Items.Add(i);
            i = new ListItem("2", "2");
            i.Attributes.Add("title", "Twice");
            retval.Items.Add(i);
            i = new ListItem("3", "3");
            i.Attributes.Add("title", "3 to 5 times");
            retval.Items.Add(i);
            i = new ListItem("4", "4");
            i.Attributes.Add("title", "6 to 10 times");
            retval.Items.Add(i);
            i = new ListItem("5", "5");
            i.Attributes.Add("title", "11 to 20 times");
            retval.Items.Add(i);
            i = new ListItem("6", "6");
            i.Attributes.Add("title", "More than 20 times");
            retval.Items.Add(i);
            i = new ListItem("7", "7");
            i.Attributes.Add("title", "Never in the past year, but it has happened before");
            retval.Items.Add(i);
            i = new ListItem("0", "0");
            i.Attributes.Add("title", "This has never happened");
            retval.Items.Add(i);
            i = new ListItem("9", "9");
            i.Attributes.Add("title", "Prefer not to answer");
            retval.Items.Add(i);

            retval.RepeatLayout = RepeatLayout.Flow;
            retval.RepeatDirection = RepeatDirection.Horizontal;
            return retval;
        }

        /// <summary>
        /// The response from the user
        /// </summary>
        /// <value></value>
        /// <remarks>AnswerText = string.Empty indicates the user refused to answer.
        /// </remarks>
        public override string AnswerText
        {
            get
            {
                if (buttonGroup1.SelectedIndex == -1)
                    buttonGroup1.SelectedIndex = 8;
                if (buttonGroup2.SelectedIndex == -1)
                    buttonGroup1.SelectedIndex = 8;
                return string.Join("|", new[] { buttonGroup1.SelectedValue, buttonGroup2.SelectedValue });
            }
            set
            {
                if (value == null)
                    return;
                string[] parts = value.Split('|');
                if (parts.Length != 2)
                    return;
                ListItem i =
                    buttonGroup1.Items.FindByValue(parts[0]);
                if (i != null)
                    i.Selected = true;
                i =
                    buttonGroup1.Items.FindByValue(parts[1]);
                if (i != null)
                    i.Selected = true;
            }
        }

        /// <summary>
        /// Called by the base class at the time it is appropriate to parse the configuration string,
        /// The configuration can be stored or used to create controls.
        /// </summary>
        protected override void ParseConfigurationString()
        {
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            HtmlGenericControl answerGroup = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            answerGroup.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "mmeaAnswerGroup");
            HtmlGenericControl questionText = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            questionText.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "mmeaQuestionText");
            Controls.Add(answerGroup);
            Controls.Add(questionText);
            questionText.Controls.Add(new LiteralControl
              {
                  Text =
                      string.Format(
                      QuestionLineFormat, QuestionIndex, QuestionText)
              });

            HtmlGenericControl answerPart1 = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            answerPart1.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "mmeaGroupOne");
            answerGroup.Controls.Add(answerPart1);
            buttonGroup1 = CreateButtonGroup();
            answerPart1.Controls.Add(new LiteralControl("You"));
            answerPart1.Controls.Add(new LiteralControl("&nbsp;"));
            answerPart1.Controls.Add(buttonGroup1);

            HtmlGenericControl answerPart2 = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            answerPart2.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "mmeaGroupTwo");
            answerGroup.Controls.Add(answerPart2);
            buttonGroup2 = CreateButtonGroup();
            // we key off the second group
            buttonGroup2.SelectedIndexChanged += SelectedIndexChanged;
            answerPart2.Controls.Add(new LiteralControl("Your partner"));
            answerPart1.Controls.Add(new LiteralControl("&nbsp;"));
            answerPart2.Controls.Add(buttonGroup2);

            HtmlGenericControl clearDiv = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            clearDiv.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "clearDiv");
            Controls.Add(clearDiv);
        }

        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            OnAnswerGiven(new AnswerEventArgs(QuestionId, AnswerText));
        }
    }
}
